<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>一键绑卡</title>
    <style>
        body {
            font-family: "微软雅黑", sans-serif;
            background-color: #e6f7ff;
            padding: 40px;
        }

        .card-form {
            width: 400px;
            margin: 100px auto;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .btn-submit {
            width: 100%;
            padding: 10px;
            background-color: #1890ff;
            color: white;
            border: none;
            font-size: 16px;
            border-radius: 5px;
            cursor: pointer;
        }

        .btn-submit:hover {
            background-color: #0078d7;
        }
    </style>
</head>
<body>
<div class="card-form">
    <h2>一键绑卡</h2>
    <form id="bindCardForm">
        <div class="form-group">
            <label for="cardNumber">银行卡号：</label>
            <input type="text" id="cardNumber" name="cardNumber" required />
        </div>
        <div class="form-group">
            <label for="bankName">发卡银行：</label>
            <input type="text" id="bankName" name="bankName" required />
        </div>
        <div class="form-group">
            <label for="cardholder">持卡人姓名：</label>
            <input type="text" id="cardholder" name="cardholder" required />
        </div>
        <button type="submit" class="btn-submit">确认绑卡</button>
    </form>
</div>

<script>
    document.getElementById('bindCardForm').addEventListener('submit', async function (e) {
        e.preventDefault();

        const formData = new FormData(this);
        const data = {};
        formData.forEach((value, key) => {
            data[key] = value;
        });

        try {
            const response = await fetch('/card/bind', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data)
            });

            const result = await response.json();

            if (result.code === 2000) {
                alert('绑卡成功！');
                window.location.href = '/UserHome.html';
            } else {
                alert(result.msg || '绑卡失败，请重试');
            }
        } catch (error) {
            console.error(error);
            alert('网络请求失败，请稍后再试');
        }
    });
</script>
</body>
</html>
